package com.superteam.mac.dao;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;

import com.superteam.mac.mode.MacUser;


public class UserDAO extends BaseDAO {

	@SuppressWarnings("unchecked")
	public List<MacUser> findAllUser()
	{
		
		List<MacUser>  list = this.getJdbcTemplate().query(
			    "select * from tbuser",
			    new RowMapper() {

			        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			        	MacUser user = new MacUser();
			          	user.setUserName(rs.getString("LogName"));
			        	user.setPassWord(rs.getString("UserName"));
			            return user;
			        }
			    });
		return list;
	}
	public MacUser findByName(String userName)
	{
		try
		{
	
		MacUser actor = (MacUser) this.getJdbcTemplate().queryForObject(
			    "select * from tbuser where LogName = ? ",
			    new Object[]{userName},
			    new RowMapper() {

			        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			        	MacUser user = new MacUser();
			        	user.setUserName(rs.getString("LogName"));
			        	user.setPassWord(rs.getString("UserName"));
			            return user;
			        }
			    });
		return actor;
		}catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	
	
	public boolean updateUser(MacUser user)
	{
		try
		{
		this.getJdbcTemplate().update(
		        "update tbuser set UserName = ? where LogName = ?", 
		        new Object[] {user.getPassWord(),user.getUserName()});
		return true;
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}

	}
}
